package ${codeMapModel.controllerNameSpace};

<#assign pkJavaType="${codeMapModel.pkJavaType}" >


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ${codeMapModel.modelNameSpace}.${codeMapModel.modelName};
import ${codeMapModel.serviceNameSpace}.I${codeMapModel.modelName}Service;

import cn.org.template.component.page.PagerHtmlModel;
import cn.org.template.component.page.PagerUtil;
import cn.org.template.component.utils.ServletUtil;

import com.google.common.collect.Maps;
/**
 * <b>description</b>:${codeMapModel.desc} <br>
 * <b>time</b>:${data} <br>
 * <b>author</b>: ${codeMapModel.author}
 */
@Controller
@RequestMapping("/${codeMapModel.lmodelName}")
public class ${codeMapModel.modelName}Controller
{
	
	ModelAndView LIST_ACTION = new ModelAndView(new RedirectView("/${codeMapModel.lmodelName}.html"));
    
	@Resource
	I${codeMapModel.modelName}Service ${codeMapModel.lmodelName}Service;
	
	/** binder用于bean属性的设置 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	/**
	 * 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}	
		
	/** 列表 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map paramMap = ServletUtil.getPageParameterMap(request);
		ModelAndView listView = new ModelAndView(ServletUtil.BASE_TEMPLATE_PATH_ADMIN+"/${codeMapModel.lmodelName}/list");
		PagerHtmlModel pager =  PagerUtil.getPagerHtmlModel(this.${codeMapModel.lmodelName}Service.getPagerModel(paramMap),
				ServletUtil.getPageParameterMap(request),ServletUtil.getURI_with_suffix("/${codeMapModel.lmodelName}"));
		listView.addObject("pager",pager);
		return listView;
	}

	/** 显示 */
	@RequestMapping(value="/{${codeMapModel.idCols[0].columnName}}")
	public String show(ModelMap model,@PathVariable ${pkJavaType} ${codeMapModel.idCols[0].columnName}) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("${codeMapModel.idCols[0].columnName}",${codeMapModel.idCols[0].columnName});
		${codeMapModel.modelName} ${codeMapModel.lmodelName} =  this.${codeMapModel.lmodelName}Service.selectById(param);
		model.addAttribute("${codeMapModel.lmodelName}",${codeMapModel.lmodelName});
		return "/${codeMapModel.lmodelName}/show";
	}

	/** 进入新增 */
	@RequestMapping(value="/new")
	public String _new(ModelMap model,${codeMapModel.modelName} ${codeMapModel.lmodelName},HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("${codeMapModel.lmodelName}",${codeMapModel.lmodelName});
		return "/${codeMapModel.lmodelName}/new";
	}
	
	/** 保存新增 */
	/** 可以考虑,加入@Valid*/
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public ModelAndView save(ModelMap model,${codeMapModel.modelName} ${codeMapModel.lmodelName},HttpServletRequest request,HttpServletResponse response) throws Exception {
		this.${codeMapModel.lmodelName}Service.insertOne(${codeMapModel.lmodelName});
		return LIST_ACTION;
	}
	
	/** 编辑信息 */
	@RequestMapping(value="/edit/{${codeMapModel.idCols[0].columnName}}")
	public String edit(ModelMap model,@PathVariable ${pkJavaType} ${codeMapModel.idCols[0].columnName}) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("${codeMapModel.idCols[0].columnName}",${codeMapModel.idCols[0].columnName});
		${codeMapModel.modelName} ${codeMapModel.lmodelName} =  this.${codeMapModel.lmodelName}Service.selectById(param);
		model.addAttribute("${codeMapModel.lmodelName}",${codeMapModel.lmodelName});
		return "/${codeMapModel.lmodelName}/edit";
	}
	
	/** 保存更新 */
	@RequestMapping(value="/update/{${codeMapModel.idCols[0].columnName}}",method=RequestMethod.POST)
	public ModelAndView update(@PathVariable ${pkJavaType} ${codeMapModel.idCols[0].columnName},${codeMapModel.modelName} ${codeMapModel.lmodelName},HttpServletRequest request,HttpServletResponse response) throws Exception {
		this.${codeMapModel.lmodelName}Service.updateById(${codeMapModel.lmodelName});
		return LIST_ACTION;
	}
	
	/** 删除 */
	@RequestMapping(value="/delete/{${codeMapModel.idCols[0].columnName}}",method=RequestMethod.GET)
	public ModelAndView delete(ModelMap model,@PathVariable ${pkJavaType} ${codeMapModel.idCols[0].columnName}) throws Exception {
		Map<String,Object> param = Maps.newHashMap();
		param.put("${codeMapModel.idCols[0].columnName}",${codeMapModel.idCols[0].columnName});
		this.${codeMapModel.lmodelName}Service.deleteById(param);
		return LIST_ACTION;
	}
	
	
}